package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author ck
 * @date 2021/10/18 19:45
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {


    @Autowired
    private ShoppingCartService shoppingCartService;


    @Override
    public R<ShoppingCart> add(ShoppingCart shoppingCart) {
//设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.get();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或者是套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {
            //添加到购物车中的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车中的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        if (one != null) {
            //如果已经存在，那么在原来的数量基础上加一
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //如果不存在，则添加到购物车中，数量默认为一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }

        return R.success(one);    }

    @Override
    public R<String> sub(ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        Long setMealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
            ShoppingCart dish = shoppingCartService.getOne(queryWrapper);
            Integer number = dish.getNumber();
            dish.setNumber(number - 1);
            if (dish.getNumber() <= 0){
                shoppingCartService.remove(queryWrapper);

            }
            shoppingCartService.updateById(dish);
        }else if (setMealId != null){
            queryWrapper.eq(ShoppingCart::getSetmealId,setMealId);
            ShoppingCart setMeal = shoppingCartService.getOne(queryWrapper);
            Integer number = setMeal.getNumber();
            setMeal.setNumber(number - 1);
            if (setMeal.getNumber() <= 0){
                shoppingCartService.remove(queryWrapper);
            }
            shoppingCartService.updateById(setMeal);
        }


        return R.success("减少商品成功");
    }
}
